package org.xhnews.ui;

import java.util.Vector;

import net.rim.device.api.system.Bitmap;
import net.rim.device.api.system.Characters;
import net.rim.device.api.system.Display;
import net.rim.device.api.ui.Color;
import net.rim.device.api.ui.DrawStyle;
import net.rim.device.api.ui.Graphics;
import net.rim.device.api.ui.Manager;
import net.rim.device.api.ui.UiApplication;
import net.rim.device.api.ui.component.ListField;
import net.rim.device.api.ui.component.ListFieldCallback;
import net.rim.device.api.ui.container.MainScreen;
import net.rim.device.api.ui.container.VerticalFieldManager;

import org.xhnews.bo.RssBo;
import org.xhnews.to.NewsItem;
import org.xhnews.to.RssChannel;
import org.xhnews.to.RssItem;

public class RssItemListScreen extends MainScreen{
	private Vector _rssitemlist;
	private RssChannel _rsschannel;
	private ListField _list;
	private TitleField _tf;
	private Bitmap readPic =  Bitmap.getBitmapResource("read.png");
	private VerticalFieldManager _fieldManager = new VerticalFieldManager(Manager.VERTICAL_SCROLL|Manager.VERTICAL_SCROLLBAR|Manager.VERTICAL_SCROLLBAR_MASK);
	public RssItemListScreen(){	    
		super(net.rim.device.api.ui.Manager.NO_VERTICAL_SCROLL);
		_tf=new TitleField();
		add(_tf);
		_rssitemlist=new Vector(); 
		ListCallback _callback = new ListCallback();
		_list = new ListField();
		_list.setRowHeight(getFont().getHeight()*2);
		_list.setCallback(_callback);
		_fieldManager.add(_list);
		add(_fieldManager);
	}
    
	public void initialize(RssChannel rsschannel)
	{
		_rsschannel=rsschannel;
		_tf.setText(rsschannel.getName());
	    reloadList();
	}
	private void reloadList()
	{
		_rssitemlist = RssBo.getNewRSSOfChannel(_rsschannel);
		_list.setSize(_rssitemlist.size());
	}
	private class ListCallback implements ListFieldCallback 
	{
	    public void drawListRow(ListField list, Graphics g, int index, int y, int w) 
	    { 
	    	RssItem temp = (RssItem)_rssitemlist.elementAt(index); 
			String title = temp.getTitle();
			String pubDate = temp.getPubDate();
			String source = temp.getSource();
			if (!temp.isRead())
				g.drawBitmap(0, y + 2, 3, list.getRowHeight() - 2, readPic, 0, 0);

			g.drawText(title, 4, y, 0, w);
	        g.setColor(Color.GREEN);
			g.drawText(source, 4, y + g.getFont().getHeight() + 2);
			g.setColor(Color.GRAY);
			g.drawText(pubDate + " ", 4, y + g.getFont().getHeight() + 2,
					DrawStyle.RIGHT, w);
			g.drawLine(4, y + g.getFont().getHeight()*2-1, getWidth(), y + g.getFont().getHeight()*2-1);
			g.setColor(Color.BLACK);
	    } 
	    public Object get(ListField list, int index) 
	    {
	        return _rssitemlist.elementAt(index); 
	    } 
	    public int indexOfList(ListField list, String prefix, int string) 
	    { 
	        return _rssitemlist.indexOf(prefix, string); 
	    } 
	    public int getPreferredWidth(ListField list) 
	    { 
	        return Display.getWidth(); 
	    } 
	}
	private void navigationToRssDescriptionScreen() {
		RssItem rssitem = (RssItem) _rssitemlist.elementAt(_list
				.getSelectedIndex());
		RssDescriptionScreen screen = new RssDescriptionScreen();
		screen.initialize(rssitem);
		UiApplication.getUiApplication().pushScreen(screen);
		
	}

	protected boolean navigationClick(int status, int time) {
		navigationToRssDescriptionScreen();
		return true;
	}

	protected boolean keyChar(char key, int status, int time) {
		if (key == Characters.ENTER) {
			navigationToRssDescriptionScreen();
		}else if(key == Characters.ESCAPE){
			onClose();
		}
		return true;
	}
}
